var database = require('../../../../data/data.js')
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false
  },
  bindChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;
    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        if (cityData[val[0]].sub[0].sub[i] != null) {
          countys.push(cityData[val[0]].sub[0].sub[i].name)
        }
      }
      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })
      return;
    }
    if (val[1] != t[1]) {
      const countys = [];
      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        if (cityData[val[0]].sub[val[1]].sub[i] != null) {
          countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
        }
      }
      if (cityData[val[0]].sub[val[1]].sub[0] != null) {
        this.setData({
          city: this.data.citys[val[1]],
          county: cityData[val[0]].sub[val[1]].sub[0].name,
          countys: countys,
          values: val,
          value: [val[0], val[1], 0]
        })
      } else {
        this.setData({
          city: this.data.citys[val[1]],
          county: "",
          countys: countys,
          values: val,
          value: [val[0], val[1], 0]
        })
      }
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  onLoad: function () {
    var that = this;
    wx.request({
      url: database.url1 + '/api/V2/mask/create_code_infinite',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        userId: database.userId
      },
      success(res) {
        that.setData({
          soleCode: res.data.data.soleCode
        })
      }
    })
    wx.request({
      url: database.url1 + '/api/V2/position/address_map',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken
      },
      success(res) {
        var cityData = res.data.data;
        const provinces = [];
        const citys = [];
        const countys = [];
        for (let i = 0; i < cityData.length; i++) {
          provinces.push(cityData[i].name);
        }
        for (let i = 0; i < cityData[0].sub.length; i++) {
          citys.push(cityData[0].sub[i].name)
        }
        for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
          if (cityData[0].sub[0].sub[i] != null) {
            countys.push(cityData[0].sub[0].sub[i].name)
          }
        }
        that.setData({
          'cityData': cityData,
          'provinces': provinces,
          'citys': citys,
          'countys': countys,
          'province': cityData[0].name,
          'city': cityData[0].sub[0].name,
          'county': cityData[0].sub[0].sub[0].name
        })
      }
    })
  },
  chooseImage: function () {
    var _this = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        if (res.tempFiles[0].size < 5242880) {
          _this.setData({
            finish1: true,
            img: res.tempFilePaths[0]
          })
        } else {
          wx.showModal({
            content: '选择的的图片文件太大，请重新选择',
          })
        }
      }
    })
  },
  check_name: function (e) {
    if (e.detail.value != "") {
      this.setData({
        finish2: true
      })
    }
  },
  check_shortname: function (e) {
    if (e.detail.value != "") {
      this.setData({
        finish3: true
      })
    }
  },
  submit: function (e) {
    wx.showLoading({
      title: '创建中',
      mask:true
    })
    var that = this;
    var citysData = this.data.cityData;
    var province_count = this.data.value[0];
    var city_count = this.data.value[1];
    var county_count = this.data.value[2];
    var province_code = citysData[province_count].code;
    var city_code = citysData[province_count].sub[city_count].code;
    var county_code = citysData[province_count].sub[city_count].sub[county_count].code;
    var that = this;
    wx.request({
      url: 'https://51.5izuqiu.cn/api/V2/softball/save_softball',
      method: "POST",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        accessToken: database.accessToken,
        soleCode: that.data.soleCode,
        userId: database.userId,
        softballId: "0",
        title: e.detail.value.title,
        shortName: e.detail.value.shortName,
        provinceId: province_code,
        cityId: city_code,
        countyId: county_code
      },
      success(res) {
        wx.uploadFile({
          url: 'https://51.5izuqiu.cn/api/V2/softball/upload_softball_logo',
          filePath: that.data.img,
          name: 'logoUrl',
          formData: {
            accessToken: database.accessToken,
            userId: database.userId,
            softballId: res.data.data_note.id,
          },
          success(res) {
            wx.hideLoading();
           wx.redirectTo({
             url: '/pages/my/myteam/myteam/myteam',
           })   
          }
        })
      },
    })
  }
})